import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import users from '../components/user/user.vue'
import rights from '../components/power/rights.vue'
import rodes from '../components/power/rodes.vue'
import Cate from '../components/goods/cate.vue'
import params from '../components/goods/params.vue'
import list from '../components/goods/goodslist.vue'
import Addgoods from '../components/goods/Add.vue'
import Order from '../components/order/order.vue'
import Report from '../components/report/report.vue'

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/', redirect: './login' },
    { path: '/login', component: login },
    {
      path: '/home',
      component: home,
      redirect: '/welcome',
      children: [
        { path: '/home', component: welcome },
        { path: '/users', component: users },
        { path: '/rights', component: rights },
        { path: '/roles', component: rodes },
        { path: '/categories', component: Cate },
        { path: '/params', component: params },
        { path: '/goods', component: list },
        { path: '/goods/add', component: Addgoods },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report },
      ]
    }
  ]
})
router.beforeEach((to, form, next) => {
  if (to.path === '/login') return next()
  const tokenstr = window.sessionStorage.getItem('token')
  if (!tokenstr) return next('/login')
  next()
})

export default router
